﻿using System;
using System.ComponentModel;
using System.IO;
using RemotingSocket;

namespace BigTestV3.common
{
    public class NetFileTransferClient : Component
    {
        #region 变量
        private NetFileTransfer _netFileTransfer = null;
        private bool _active = false;
        private string _noticeRegisterMethodName = "tcp://192.168.12.205:8085/FileService";
        private int _port = 8085;
        #endregion

        #region 属性
        public int Port
        {
            get { return this._port; }
            set { this._port = value; }
        }

        public string NoticeRegisterMethodName
        {
            get { return this._noticeRegisterMethodName; }
            set { this._noticeRegisterMethodName = value; }
        }

        public bool Active
        {
            get { return this._active; }
        }

        public NetFileTransfer NetFileTransferObject
        {
            get
            {
                return this._netFileTransfer;
            }
        }
        #endregion

        #region 方法
        public bool Connect()
        {
            try
            {
                _netFileTransfer = (NetFileTransfer)Activator.GetObject(
                    typeof(RemotingSocket.NetFileTransfer), _noticeRegisterMethodName);
                if (_netFileTransfer != null && _netFileTransfer.ToString().Length > 1)
                {
                    this._active = true;
                    return true;
                }
                else
                {
                    this._active = false;
                    return false;
                }
            }
            catch
            {
                this._active = false;
                return false;
            }
        }

        public void Disconnection()
        {
            _netFileTransfer = null;
            _active = false;
        }

        public byte[] GetFileBytes(string remoteFilePath)
        {
            if (!_active) return null;
            try
            {
                return _netFileTransfer.GetFileBytes(remoteFilePath);
            }
            catch
            {
                return null;
            }
        }

        public bool GetFile(string remoteFilePath, string localSavePath)
        {
            if (!_active) return true;
            try
            {
                byte[] filebytes = _netFileTransfer.GetFileBytes(remoteFilePath);
                if (filebytes != null)
                {
                    using (FileStream fs = new FileStream(localSavePath, FileMode.CreateNew, FileAccess.Write,
                                                    FileShare.ReadWrite))
                    {
                        fs.Write(filebytes, 0, filebytes.Length);
                        fs.Close();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool SendFileBytes(byte[] fileBytes, string remoteSavePath)
        {
            if (!_active) return false;
            try
            {
                return _netFileTransfer.SendFileBytes(remoteSavePath, fileBytes);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 将本地文件发送至远程机器
        /// </summary>
        /// <param name="localFilePath">本地文件路径</param>
        /// <param name="remoteSavePath">远程要保存的文件路径</param>
        /// <returns></returns>
        public bool SendFile(string localFilePath, string remoteSavePath)
        {
            if (!_active) return false;
            try
            {
                if (!File.Exists(localFilePath)) return false;

                using (FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    fs.Close();

                    return _netFileTransfer.SendFileBytes(remoteSavePath, buffer);
                }

            }
            catch
            {
                return false;
            }
        }

        public void SendMessage(string msg)
        {
            //  _netFileTransfer = (NetFileTransfer)Activator.GetObject(typeof(RemotingSocket.NetFileTransfer), _noticeRegisterMethodName);
            _netFileTransfer.SendMessage(msg);
        }
        #endregion
    }

}